﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using HousingInfo.Library;

namespace WebHousingInfo
{
    public partial class QuanlytinDoanhNghiep : System.Web.UI.Page
    {
        private static string mode = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserName"] == null)
                    Response.Redirect("DangNhap.aspx");
                mode = Request.QueryString["xemtindoanhnghiep"];
                BindGrid();
            }
        }

        public void GettotalNewsBussiness()
        {
            lblchoduyet_bussiness.Text = "[" + NewsBussinessBOList.GetType_NewsBussinessBOList_ByUserName(Session["UserName"].ToString(), false, false, true, false).Count.ToString() + "]";
            lbldangdang_bussiness.Text = "[" + NewsBussinessBOList.GetType_NewsBussinessBOList_ByUserName(Session["UserName"].ToString(), false, true, false, false).Count.ToString() + "]";
            lblhethan_bussiness.Text = "[" + NewsBussinessBOList.GetType_NewsBussinessBOList_ByUserName(Session["UserName"].ToString(), false, false, false, true).Count.ToString() + "]";
            lbltatca_bussiness.Text = "[" + NewsBussinessBOList.GetType_NewsBussinessBOList_ByUserName(Session["UserName"].ToString(), true, false, false, false).Count.ToString() + "]";
            lblvanbanall.Text = "[" + TextHouseBOList.GetTextHouse_ByUser(Session["UserName"].ToString(), "tatca").Count.ToString() + "]";
            lblvanbandang.Text = "[" + TextHouseBOList.GetTextHouse_ByUser(Session["UserName"].ToString(), "dangdang").Count.ToString() + "]";
            lblvanbanduyet.Text = "[" + TextHouseBOList.GetTextHouse_ByUser(Session["UserName"].ToString(), "choduyet").Count.ToString() + "]";
        }

        protected void btnketthuc_Click(object sender, EventArgs e)
        {
            bool test = false;
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                if (chkSel.Checked == true)
                {
                    try
                    {
                        test = true;
                        int bussinessid = Convert.ToInt32(gvRow.Cells[2].Text);
                        NewsBussinessBO newsbussinessbo = NewsBussinessBO.GetNewsBussinessBO(bussinessid);
                        newsbussinessbo.IsActive = false;
                        newsbussinessbo.Save();
                    }
                    catch
                    {
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Kết thúc đăng tin thất bại !')", true);
                        BindGrid();
                        return;
                    }
                }
            }
            if (test == false)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Chưa chọn tin Doanh Nghiệp cần kết thúc đăng tin !')", true);
                return;
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Đã kết thúc đăng tin của các tin Doanh Nghiệp được chọn !')", true);
                BindGrid();
            }
        }

        protected void btnxoa_Click(object sender, EventArgs e)
        {
            bool test = false;
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                if (chkSel.Checked == true)
                {
                    try
                    {
                        test = true;
                        int bussinessid = Convert.ToInt32(gvRow.Cells[2].Text);
                        NewsBussinessBO newsbussinessbo = NewsBussinessBO.GetNewsBussinessBO(bussinessid);
                        deleteExitsImage(newsbussinessbo.UrlImage);
                        NewsBussinessBO.DeleteNewsBussinessBO(bussinessid);
                    }
                    catch
                    {
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Xoá thất bại!!!')", true);
                        BindGrid();
                        return;
                    }
                }
            }
            if (test == false)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Chưa chọn thông tin cần xóa !!!')", true);
                return;
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Xoá thành công!!!')", true);
                BindGrid();
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.BindGrid();
        }

        public void BindGrid()
        {
            GettotalNewsBussiness();
            if (mode == "choduyet")
            {
                lbltin.Text = "TIN CHỜ DUYỆT";
                GridView1.DataSource = NewsBussinessBOList.GetType_NewsBussinessBOList_ByUserName(Session["UserName"].ToString(), false, false, true, false);
                GridView1.DataBind();
            }
            else if (mode == "dangdang")
            {
                lbltin.Text = "TIN ĐANG ĐĂNG";
                GridView1.DataSource = NewsBussinessBOList.GetType_NewsBussinessBOList_ByUserName(Session["UserName"].ToString(), false, true, false, false);
                GridView1.DataBind();
            }
            else if (mode == "hethan")
            {
                lbltin.Text = "TIN HẾT HẠN";
                GridView1.DataSource = NewsBussinessBOList.GetType_NewsBussinessBOList_ByUserName(Session["UserName"].ToString(), false, false, false, true);
                GridView1.DataBind();
            }
            else
            {
                lbltin.Text = "TẤT CẢ TIN";
                GridView1.DataSource = NewsBussinessBOList.GetType_NewsBussinessBOList_ByUserName(Session["UserName"].ToString(), true, false, false, false);
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Xem")
            {
                GridViewRow selectrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                string Title = ((LinkButton)(((Control)e.CommandSource).Parent.Parent).FindControl("lbtname")).Text;
                Response.Redirect("TinDoanhNghiepChiTiet.aspx?iddn=" + selectrow.Cells[2].Text);
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                string strScript = "SelectDeSelectHeader(" + ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).ClientID + ");";
                ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Attributes.Add("onclick", strScript);
            }
            catch { }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("DangTinDoanhNghiep.aspx?iddn=" + GridView1.SelectedRow.Cells[2].Text + "&back=" + mode);
        }

        private void deleteExitsImage(string filedelete)
        {
            try
            {
                var pathFileImage = Server.MapPath("TotalUpload/ImagesTotal/" + filedelete);
                FileInfo fileInfo;
                if (File.Exists(pathFileImage))
                {
                    fileInfo = new FileInfo(pathFileImage);
                    fileInfo.Delete();
                }
            }
            catch
            {
            }
        }
    }
}
